package com.zretc.controller;

import com.github.pagehelper.PageInfo;
import com.zretc.entity.Cinema;
import com.zretc.entity.Movie;
import com.zretc.service.CinemaBackService;
import com.zretc.service.MovieFirstPageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/getMovieInCinema")
public class CinemaBackController {

    private static  final Logger logger = LoggerFactory.getLogger(CinemaBackController.class);

    @Autowired
    private CinemaBackService cinemaBackService;

    @Autowired
    private MovieFirstPageService movieFirstPageService;

    @RequestMapping("/getCinemaList")
    @ResponseBody
    public List<Cinema> getCinema(){

        // 调用业务层
        List<Cinema> cinemaList = cinemaBackService.getCinema();
        return cinemaList;
    }

    @RequestMapping("/comeCinema")
    @ResponseBody
    public PageInfo<Movie> getMovieInCinema(@RequestParam(value = "pageNum") String pageNum,
                                            @RequestParam(value = "pageSize") String pageSize,
                                            @RequestParam(value = "cinemaName") String cinemaName){
        // 2.调用业务层
        Map<String,String> map = new HashMap<>();

        map.put("pageNum",pageNum);
        map.put("pageSize",pageSize);
        map.put("cinemaName",cinemaName);

        PageInfo<Movie> movieById = movieFirstPageService.getMovieById(map);

        //logger.info(movieByStatus.toString());
        /*HttpSession session = request.getSession();
        session.setAttribute("cinemaName", cinemaName);*/

        return movieById;
    }

    //@ResponseBody
    @RequestMapping("/updateShowStatus")
    public String updateMovieShowStatus(@RequestParam(value="movieName") String movieName){
        boolean updateMovieShowStatus = movieFirstPageService.updateMovieShowStatus(movieName);

        if(updateMovieShowStatus == true){
            return "forward:searchMovieByName";
        }else{
            return "forward:skip";
        }
    }

    @ResponseBody
    @RequestMapping("/searchMovieByName")
    public PageInfo<Movie> getMovieByCinemaAndMovieName(@RequestParam(value = "pageNum") String pageNum,
                                                        @RequestParam(value = "pageSize") String pageSize,
                                                        @RequestParam(value = "cinemaName") String cinemaName,
                                                        @RequestParam(value = "movieName") String movieName){

        // 2.调用业务层
        Map<String,String> map = new HashMap<>();

        map.put("pageNum",pageNum);
        map.put("pageSize",pageSize);
        map.put("cinemaName",cinemaName);
        map.put("movieName",movieName);

        PageInfo<Movie> movieListPage = cinemaBackService.getMovieByCinemaAndMovieName(map);

        return movieListPage;
    }

    @RequestMapping("/skip")
    public String skip(){
        return "redirect:../back/cinema_manage.jsp";
    }
}
